<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
</head>
<body>
    <table id="demo" lay-filter="test" ></table>
<!--管理员管理权限-->
<script type="text/html" id="checkAdmi">
    <input type="checkbox" name="admi" admi_id="{{ d.id }}"  lay-skin="switch" lay-text="T|F" value="{{ d.admi }}" title="" lay-filter="admi" {{ d.admi == 'true' ? 'checked' : '' }}>
</script>
<!--商品管理权限-->
<script type="text/html" id="checkGoods">
    <input type="checkbox" name="goods" admi_id="{{ d.id }}" lay-skin="switch" lay-text="T|F" value="{{ d.goods }}" title="" lay-filter="goods" {{ d.goods == 'true' ? 'checked' : '' }}>
</script>
<!--管理员管理--添加管理员权限-->
<script type="text/html" id="checkAdmi_add">
    <input type="checkbox" name="admi_add" admi_id="{{ d.id }}" lay-skin="switch" lay-text="T|F" value="{{ d.admi_add }}" title="" lay-filter="admi_add" {{ d.admi_add == 'true' ? 'checked' : '' }}>
</script>
<!--管理员管理--管理员权限-->
<script type="text/html" id="checkAdmi_admi">
    <input type="checkbox" name="admi_admi" admi_id="{{ d.id }}" lay-skin="switch" lay-text="T|F" value="{{ d.admi_admi }}" title="" lay-filter="admi_admi" {{ d.admi_admi == 'true' ? 'checked' : '' }}>
</script>
<!--商品管理--商品详情权限-->
<script type="text/html" id="checkGoods_details">
    <input type="checkbox" name="goods_details" admi_id="{{ d.id }}" lay-skin="switch" lay-text="T|F" value="{{ d.goods_details }}" title="" lay-filter="goods_details" {{ d.goods_details == 'true' ? 'checked' : '' }}>
</script>
<!--商品管理--商品分类权限-->
<script type="text/html" id="checkGoods_classify">
    <input type="checkbox" name="goods_classify" admi_id="{{ d.id }}" lay-skin="switch" lay-text="T|F" value="{{ d.goods_classify }}" title="" lay-filter="goods_classify" {{ d.goods_classify == 'true' ? 'checked' : '' }}>
</script>
<!--管理员管理--管理员权限-->
<script type="text/html" id="checkAdmi_operate">
    <input type="checkbox" name="admi_operate" admi_id="{{ d.id }}" lay-skin="switch" lay-text="T|F" value="{{ d.admi_operate }}" title="" lay-filter="admi_operate" {{ d.admi_operate == 'true' ? 'checked' : '' }}>
</script>





<script src="../layui/layui.js"></script>
<script src="../js/ajax.js"></script>
<script>
layui.use('table', function(){
    var table = layui.table
    ,form = layui.form;
    //表格初始化
    table.render({
        elem: '#demo'
        ,height: 400
        ,url: './public/select_limit.php?table=admi_power' //数据接口
        ,page: true //开启分页
        ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
        ,cols: [[ //表头
        {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left' }
        ,{field: 'login_name', title: '登录名', width:80}
        ,{field: 'login_id', title: '登录id', width:80, }
        ,{field: 'admi', title: '管理员管理权限', width:130, templet: '#checkAdmi'} 
        ,{field: 'goods', title: '商品管理权限', width: 130, templet: '#checkGoods'}
        ,{field: 'admi_add', title: '管理员管理--添加管理员权限', width: 180, templet: '#checkAdmi_add' }
        ,{field: 'admi_admi', title: '管理员管理--管理员权限', width: 180, templet: '#checkAdmi_admi' }
        ,{field: 'goods_details', title: '商品管理--商品详情权限', width: 180, templet: '#checkGoods_details'}
        ,{field: 'goods_classify', title: '商品管理--商品分类权限', width: 180, templet: '#checkGoods_classify'}
        ,{field: 'admi_operate', title: '管理员管理--管理员权限权限', width: 180, templet: '#checkAdmi_operate'}
        ]]
        ,done: function(res, curr, count){ 
            //console.log(res);   // 后台返回来的数据
            //console.log(curr);
            //console.log(count);
        }
    });


    //console.log(table)


    // 监听锁定
    form.on('switch(admi)', function(obj){
        switchOn(this, obj);
        //console.log(this)
        //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
        // console.log(obj.value)
        //console.log(table)
    });
    form.on('switch(goods)', function(obj){
        switchOn(this);
    });
    form.on('switch(admi_add)', function(obj){
        switchOn(this);
    });
    form.on('switch(admi_admi)', function(obj){
        switchOn(this);
    });
    form.on('switch(goods_details)', function(obj){
        switchOn(this);
    });
    form.on('switch(goods_classify)', function(obj){
        switchOn(this);
    });
    form.on('switch(admi_operate)', function(obj){
        switchOn(this);
    });

    function switchOn(This, obj){
        var index2 = layer.load(2, {time: 10*1000});
        if(This.checked){
            This.value = 'true';
        }else{
            This.value = 'false';
        }
        
        var id = This.attributes['admi_id'].value;
        var field = This.name;
        var field_val = This.value;
        
        ajax({
            url: './public/form_switch.php',
            data: {
                table: 'admi_power',
                id_name: 'id',
                id: id,
                field: field,
                field_val: field_val
            },
            method: 'post',
            success: function(res){
                //table.reload();
                //location.reload();
                // 关闭加载动画
                layer.close(index2); 
                if(res != 1){
                    layer.alert('执行失败, 请联系相关人员处理!');
                    form.render('checkbox');
                }
            }
        });
    }
});

</script>
</body>
</html>